package com.trainingcloud.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.trainingcloud.mapper.ClassInfoMapper;
import com.trainingcloud.mapper.StudentInfoMapper;
import com.trainingcloud.mapper.UserMapper;
import com.trainingcloud.pojo.ClassInfo;
import com.trainingcloud.pojo.ExcelUserData;
import com.trainingcloud.pojo.StudentInfo;
import com.trainingcloud.pojo.User;
import com.trainingcloud.service.IUserService;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 纸鹤
 * @since 2022-10-27
 */
public class ExcelUserReadListener implements ReadListener<ExcelUserData> {

    private UserMapper userMapper;

    private ClassInfoMapper classInfoMapper;

    private StudentInfoMapper studentInfoMapper;

    public ExcelUserReadListener(UserMapper userMapper,ClassInfoMapper classInfoMapper,StudentInfoMapper studentInfoMapper){
        this.userMapper = userMapper;
        this.classInfoMapper = classInfoMapper;
        this.studentInfoMapper = studentInfoMapper;
    }

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    /**
     * 缓存的数据
     */
    private List<ExcelUserData> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    /**
     * 真正存储数据的对象集合
     */
    private List<User> dataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param data    one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */
    @Override
    public void invoke(ExcelUserData data, AnalysisContext context) {
        System.err.println(data);
        cachedDataList.add(data);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
    }

    /**
     * 加上存储数据库
     */
    private void saveData(){
        User user = new User();
        user.setUserType(0);
        user.setPassword("123456");
        QueryWrapper<ClassInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",cachedDataList.get(0).getClassName());
        ClassInfo classInfo = classInfoMapper.selectOne(queryWrapper);
        int classId = classInfo.getId();
        for (ExcelUserData data : cachedDataList){
            //补充真正的userlist
            user.setStatus(classId);
            user.setUserNick(data.getStuName());
            user.setUserPhone(data.getPhone());
            user.setUserName(data.getUserName());
            user.setUserGender(data.getSex()=="男"?1:0);
            //dataList.add(user);
            int i = userMapper.insert(user);
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setRelationClassId(user.getStatus());
            studentInfo.setUserId(user.getId());
            studentInfoMapper.insert(studentInfo);
        }
        //datalist
        //System.out.println(dataList);
    }
}
